What is rollup-plugin-node-resolve?
The rollup-plugin-node-resolve package is a Rollup plugin that allows you to use the Node.js resolution algorithm to locate modules using the Node.js module resolution algorithm, which includes resolving modules from node_modules. This is particularly useful for bundling dependencies from npm.
What are rollup-plugin-node-resolve's main functionalities?
Basic Usage
This basic setup demonstrates how to use the rollup-plugin-node-resolve to resolve modules from node_modules in a Rollup configuration.
const resolve = require('@rollup/plugin-node-resolve');
module.exports = {
input: 'src/index.js',
output: {
file: 'bundle.js',
format: 'cjs'
},
plugins: [resolve()]
};
Custom Extensions
This example shows how to configure the plugin to resolve additional file extensions such as .mjs, .json, and .node.
const resolve = require('@rollup/plugin-node-resolve');
module.exports = {
input: 'src/index.js',
output: {
file: 'bundle.js',
format: 'cjs'
},
plugins: [
resolve({
extensions: ['.mjs', '.js', '.json', '.node']
})
]
};
Using with CommonJS
This example demonstrates how to use rollup-plugin-node-resolve in conjunction with rollup-plugin-commonjs to bundle CommonJS modules.
const resolve = require('@rollup/plugin-node-resolve');
const commonjs = require('@rollup/plugin-commonjs');
module.exports = {
input: 'src/index.js',
output: {
file: 'bundle.js',
format: 'cjs'
},
plugins: [
resolve(),
commonjs()
]
};
Other packages similar to rollup-plugin-node-resolve
rollup-plugin-commonjs
The rollup-plugin-commonjs package allows Rollup to convert CommonJS modules to ES6, so they can be included in a Rollup bundle. It is often used together with rollup-plugin-node-resolve to handle npm packages that are written in CommonJS.
rollup-plugin-alias
The rollup-plugin-alias package allows you to define and use custom module aliases in your Rollup configuration. This can be useful for simplifying import paths and managing dependencies more effectively.
rollup-plugin-json
The rollup-plugin-json package allows Rollup to import JSON files, converting them to ES6 modules. This is useful for including configuration files or other data in your bundle.
rollup-plugin-node-resolve
This plugin used to be called rollup-plugin-npm
Locate modules using the Node resolution algorithm, for using third party modules in node_modules
Installation
npm install --save-dev rollup-plugin-node-resolve
Usage
import resolve from 'rollup-plugin-node-resolve';
export default {
input: 'main.js',
output: {
file: 'bundle.js',
format: 'iife',
name: 'MyModule'
},
plugins: [
resolve({
mainFields: ['module', 'main'],
module: true,
jsnext: true,
main: true,
browser: true,
extensions: [ '.mjs', '.js', '.jsx', '.json' ],
preferBuiltins: false,
jail: '/my/jail/path',
only: [ 'some_module', /^@some_scope\/.*$/ ],
modulesOnly: true,
dedupe: [ 'react', 'react-dom' ],
customResolveOptions: {
moduleDirectory: 'js_modules'
}
})
]
};
Using with rollup-plugin-commonjs
Since most packages in your node_modules folder are probably legacy CommonJS rather than JavaScript modules, you may need to use rollup-plugin-commonjs:
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
export default {
input: 'main.js',
output: {
file: 'bundle.js',
format: 'iife'
},
name: 'MyModule',
plugins: [
resolve(),
commonjs()
]
};
Resolving Built-Ins (like fs
)
This plugin won't resolve any builtins (e.g. fs
). If you need to resolve builtins you can install local modules and set preferBuiltins
to false
, or install a plugin like rollup-plugin-node-builtins which provides stubbed versions of these methods.
If you want to silence warnings about builtins, you can add the list of builtins to the externals
option; like so:
import resolve from 'rollup-plugin-node-resolve';
import builtins from 'builtin-modules'
export default ({
input: ...,
plugins: [resolve()],
externals: builtins,
output: ...
})
License
MIT